/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sales.hanserver.action.hancadso.purchaserequest;

import br.com.sales.hanserver.action.hancadso.purchaserequest.subitem.GetDataSubItemAction;
import static br.com.sales.hanserver.dao.Dao.closeConn;
import static br.com.sales.hanserver.dao.Dao.getConn;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author v
 */
public class UpdateRequester {

    public UpdateRequester() {
    }

    public void update(Timestamp date, String field, Long nrSequence) {
        try {
            Connection conn = getConn();
            String update = "";
            if (field.equals("DT_APROVACAO")) {
                update = " update SOLICITACAO_COMPRA set  dt_aprovacao = ? where nr_sequencia = ?";
            } else if (field.equals("DT_ENCERRAMENTO")) {
                update = " update SOLICITACAO_COMPRA set  dt_encerramento = ? where nr_sequencia = ?";
                String updateProduc = " update PRODUTO set  qt_estoque = (qt_estoque + ?) where nr_sequencia = ?";

                List<Map> list = new GetDataSubItemAction().getDataIDGrid(nrSequence);
                for (Map map : list) {
                    PreparedStatement preparedStmt = conn.prepareStatement(updateProduc);
                    preparedStmt.setInt(1, (Integer) map.get("QT_QUANTIDADE"));
                    preparedStmt.setLong(2, (Long) map.get("NR_SEQ_PRODUTO"));
                    preparedStmt.executeUpdate();
                }

            } else if (field.equals("DT_CANCELAMENTO")) {
                update = " update SOLICITACAO_COMPRA set  dt_cancelamento = ? where nr_sequencia = ?";
            }

            PreparedStatement preparedStmt = conn.prepareStatement(update);
            preparedStmt.setTimestamp(1, date);
            preparedStmt.setLong(2, nrSequence);

            preparedStmt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(UpdateRequester.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            closeConn();
        }
    }
}
